<template>
	<view class="visit">
		<view v-if="!boxHide">
			<block v-if="type != 2">
				<view class="add_item">
					<view>预约下次拜访时间</view>
					<Time :fields="'second'" :lang="false" :inputName="'预约下次拜访时间'" :time="upJson.appointment_date" @getDate="getStartDate"></Time>
				</view>
			</block>
			
			<view class="add_item" v-if="type != 2">
				<view>拜访类型</view>
				<picker mode="selector" :range="visitLaber" @change="getInputValue" class="add_picker">
					<text>{{upJson.type}}</text>
					<image src="/static/images/right.png" class="right"></image>
				</picker>
			</view>
			
			<view class="add_item" v-if="type == 1 || type == 5">
				<view>拜访主题</view>
				<input type="text" placeholder="请输入拜访主题" v-model="upJson.title"/>
			</view>
			
			<view class="add_textarea" v-if="type != 2">
				<view>拜访内容</view>
				<textarea placeholder="请输入拜访内容" v-model="upJson.content"/>
			</view>
			
			<view class="add_textarea" v-if="type == 2">
				<view>留言</view>
				<textarea placeholder="请输入留言内容" v-model="content"/>
			</view>
			
			<view class="save" @tap="save(type)">{{type == 0 ? '确认完成' : '确认'}}</view>
		</view>
		<Loading ref="load"></Loading>
	</view>
</template>

<script>
	import Time from "../../../../components/time/time.vue";
	import Loading from "../../../../components/loading/loading.vue"
	export default{
		components:{
			Time,
			Loading
		},
		data(){
			return{
				type:0,
				title:['完成拜访','修改拜访','留言','','','添加拜访'],
				visitLaber:[],
				upJson:{
					customer_id:"",
					title:"",
					content:"",
					appointment_date:"",
					user_id:uni.getStorageSync("userId"),
					type:""
				},
				visitId:"",
				boxHide:true,
				content:""
			}
		},
		onLoad(options) {
			console.log(options)
			let that = this;
			this.upJson.customer_id = Number(options.merchantId);
			this.visitId = options.visitId;
			this.type = Number(options.type);
			uni.setNavigationBarTitle({
				title:that.title[this.type]
			})
			this.$api.getClientVisitType({}).then(res =>{
				this.visitLaber = res;
				this.upJson.type = res[0]
			})
		},
		onShow() {
			if(this.type != 2 && this.type != 5){
				this.$refs.load.initLoading({loadType:1,loadPrompt:false}).then(res =>{
					this.$api.addClientVisit({id:this.visitId},0).then(res =>{
						this.upJson = res;
						this.boxHide = false;
						this.$refs.load.getLoadingData({})
					})
				})
			}else{
				this.boxHide = false;
			}
		},
		methods:{
			getStartDate(res){
				console.log(res)
				this.upJson.appointment_date = res.date
			},
			getInputValue(e){
				console.log(this.upJson)
				this.upJson.type = this.visitLaber[e.detail.value];
			},
			save(type){
				if(type == 5 && this.upJson.title.length == 0){
					this.$api.prompt("标题不能为空");
				}else if(type == 5 && this.upJson.content.length == 0){
					this.$api.prompt("拜访内容不能为空");
				}else{
					let upJson = {};
					for(let i in this.upJson){
						upJson[i] = this.upJson[i];
					}
					if(this.type == 0){
						upJson.status = 2;
						this.$api.addClientVisit(upJson,1);
					}else if(this.type == 2){
						if(this.content.length == 0){
							this.$api.prompt("留言内容不能为空");
						}else{
							this.$app.globalData.upData.clientInfo[3] = true;
							this.$api.addClientVisit({visit_id:this.visitId,user_id:uni.getStorageSync("userId"),content:this.content},this.type);
						}
					}else{
						this.$app.globalData.upData.clientInfo[3] = true;
						this.$api.addClientVisit(upJson,this.type);
					}
				}
			}
		}
	}
</script>

<style>
	.visit{background: #FFFFFF;padding-bottom: 50upx;}
</style>
